package com.fdb.efp.limit.service.common;

import java.math.BigDecimal;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fdb.efp.console.service.vo.PrdInfoVO;
import com.fdb.efp.console.service.vo.PrdParamVO;
import com.fdb.efp.console.service.vo.PrdRateStdVO;
import com.fdb.efp.console.service.vo.RateSchemeDetailVO;
import com.fdb.basic.framework.core.constant.BaseConstant;
import com.fdb.basic.framework.core.constant.CurrencyConstant;
import com.fdb.basic.framework.core.util.BigDecimalUtil;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.limit.common.LimitConstant;
import com.fdb.efp.limit.service.domain.LmtIndivApp;
import com.fdb.efp.limit.service.domain.LmtPrelist;


public class LmtIndivAppUtil {
	
    private static Logger logger = LoggerFactory.getLogger(LmtIndivAppUtil.class);
    
	/**
	 * 正式表中获取数据插入授信申请表中
	 * 
	 * @param lmtIndivApp
	 * @param lmtPrelist
	 * @return
	 */
	public static LmtIndivApp setLmtIndivAppByLmtPrelist(LmtIndivApp lmtIndivApp, LmtPrelist lmtPrelist) {
		lmtIndivApp.setCusId(lmtPrelist.getCusId());
		lmtIndivApp.setCusName(lmtPrelist.getCusName());
		lmtIndivApp.setCertCode(lmtPrelist.getCertCode());
		lmtIndivApp.setCertType(lmtPrelist.getCertType());
		lmtIndivApp.setPrdId(lmtPrelist.getPrdId());
		lmtIndivApp.setApplyAmt(LimitConstant.PRELMT_TYPE_1.equals(lmtPrelist.getLmtType())?lmtPrelist.getWaitActAmt():null);//额度申请时插入为null
		lmtIndivApp.setApproveAmt(LimitConstant.PRELMT_TYPE_1.equals(lmtPrelist.getLmtType())?lmtPrelist.getWaitActAmt():null);//批复额度 申请时null,申请成功后回写
		lmtIndivApp.setApplyTerm(lmtPrelist.getTerm());//申请期限
		lmtIndivApp.setApproveTerm(lmtPrelist.getTerm());//批复期限
		lmtIndivApp.setTermType(lmtPrelist.getTermType());//期限类型
		lmtIndivApp.setValidDays(lmtPrelist.getActValidDays());//授信批复有效期
		lmtIndivApp.setApplyRateY(lmtPrelist.getFixedRate());// 申请利率
	    lmtIndivApp.setApproveRateY(LimitConstant.PRELMT_TYPE_1.equals(lmtPrelist.getLmtType())?lmtPrelist.getFixedRate():null);//插入时为null,申请成功后回写 //批复年利率
		lmtIndivApp.setCurrencyType(CurrencyConstant.CNY);//币种
		lmtIndivApp.setCusManager(lmtPrelist.getCusManager());//客户经理
		lmtIndivApp.setMainBrId(lmtPrelist.getMainBrId());//管理机构
		lmtIndivApp.setChargeoffBrId(lmtPrelist.getChargeoffBrId());//出账机构
		lmtIndivApp.setLegalOrgCode(lmtPrelist.getLegalOrgCode());//法人机构号
		lmtIndivApp.setLegalOrgName(lmtPrelist.getLegalOrgName());//法人机构名称
		lmtIndivApp.setApplyDate(null);//申请日期 取营业日期
		lmtIndivApp.setActSts(LimitConstant.ACT_STS_0);//激活状态   lmtPrelist.getStatus()
		lmtIndivApp.setCreateTime(TimeUtil.getCurrentDateTime());
		lmtIndivApp.setCreateUser(lmtPrelist.getCusManager());//客户经理
		lmtIndivApp.setLastUpdateTime(TimeUtil.getCurrentDateTime());
		lmtIndivApp.setLastUpdateUser(lmtPrelist.getCusManager());//最后更新人
		lmtIndivApp.setInvestigatorId(null);//审查人
		lmtIndivApp.setEndorseId(null);//审批人
		lmtIndivApp.setEndorseDate(null);//审批日期   (取营业日期)
		//lmtIndivApp.setApproveStatus(LimitConstant.PRELMT_TYPE_1.equals(lmtPrelist.getLmtType())?BaseConstant.APPR_STATUS_997:BaseConstant.APPR_STATUS_000);
		lmtIndivApp.setApproveStatus(BaseConstant.APPR_STATUS_000);
		lmtIndivApp.setIsCreInfo(BaseConstant.YES);//是否同意查征信
		lmtIndivApp.setLmtType(lmtPrelist.getLmtType());//额度类型
		lmtIndivApp.setAssureMeansMain(null);//主担保方式
		lmtIndivApp.setAssureMeans2(null);//第二担保方式
		lmtIndivApp.setAssureMeans3(null);//第三担保方式
		return lmtIndivApp;
	}
	
	/**
	 * 封装产品信息查询VO
	 * @param lmtPrelist
	 * @return
	 */
	public static PrdParamVO getPrdParamVO(LmtPrelist lmtPrelist) {
		PrdParamVO prdParamVO = new PrdParamVO();
		if (lmtPrelist!=null) {
			prdParamVO.setPrdId(lmtPrelist.getPrdId());
			prdParamVO.setAmtApprFlow(true);
			prdParamVO.setAmtMeaMedol(true);
			prdParamVO.setContTemplate(true);
			prdParamVO.setCrdApprFlow(true);
			prdParamVO.setCrdMeaMedol(true);
			prdParamVO.setCreditForm(true);
			prdParamVO.setEleNucForm(true);
	        return prdParamVO;
		}else {
			return null;
		}
		
	}
	
	//浮动方式 插入年利率
	public static LmtIndivApp setRateY(LmtIndivApp lmtIndivApp,PrdRateStdVO prdRateStdVO,LmtPrelist lmtPrelist) {
		if (prdRateStdVO.getRate()!=null && lmtIndivApp!=null&&lmtPrelist!=null) {
			BigDecimal baseRate = prdRateStdVO.getRate();
			
    		String floatWay = lmtPrelist.getIrFloatType();//浮动方式
    		BigDecimal floatPct = lmtPrelist.getIrFloatPct();//比例
    		BigDecimal rateY = null;
    		if (BaseConstant.IR_FLOAT_TYPE_UP.equals(floatWay)) {
    			rateY = baseRate.multiply(BigDecimal.ONE.add(floatPct));
    		}else {
    			rateY = baseRate.multiply(BigDecimal.ONE.subtract(floatPct));
    		}
    		
    		//利率，截取小数点后6位
    		rateY = BigDecimalUtil.cutOutDecimal(rateY, 6);
    		
    		lmtIndivApp.setApplyRateY(rateY);
    		lmtIndivApp.setApproveRateY(rateY);
		}
		return lmtIndivApp;
	} 
	
	
	
	
	/**
	 * 插入从产品种获取参数
	 * @param lmtIndivApp
	 * @param lmtPrelist
	 * @param prdInfoVO
	 * @return
	 */
	public static LmtIndivApp setLmtIndivAppByPrdInfo(
			LmtIndivApp lmtIndivApp,
			LmtPrelist lmtPrelist,
			PrdInfoVO prdInfoVO
			){
		if (prdInfoVO==null) {
			return lmtIndivApp;
		}
		lmtIndivApp.setCyclicFlg(prdInfoVO.getCyclicWay());//是否循环
		lmtIndivApp.setPrdCode(prdInfoVO.getPrdCode());//产品编码
		lmtIndivApp.setPrdName(prdInfoVO.getPrdName());//产品名称
		lmtIndivApp.setPrdType(prdInfoVO.getPrdThlDri());//产品类型
		lmtIndivApp.setAssureMeansMain(prdInfoVO.getGuarWay());//主担保方式
		lmtIndivApp.setRepaymentMode(prdInfoVO.getRepayWay());//还款类型
		lmtIndivApp.setRepaymentPeriod(new BigDecimal(prdInfoVO.getRepayCycle()));//还款周期
		lmtIndivApp.setRepaymentPeriodType(prdInfoVO.getRepayCycleType());//还款周期类型
		lmtIndivApp.setRepaymentDay(new BigDecimal(prdInfoVO.getRepayDay()));//。还款日
		lmtIndivApp.setRepaymentDayType(prdInfoVO.getRepayDayType());//还款日类型
		lmtIndivApp.setLoanUseSub(prdInfoVO.getLoanUseType());//贷款用途
		lmtIndivApp.setRepaymentSrcDec("个人收入");//稍后会进行修改
		lmtIndivApp.setCaseCode(prdInfoVO.getCaseCode());//专案代码
		lmtIndivApp.setCaseName(prdInfoVO.getCaseName());//专案名称
		//个人收入
		lmtIndivApp.setLoanUseType(prdInfoVO.getLoanUseGenere());//贷款用途大类
		return lmtIndivApp;
	}
	
	
	/**
	 * 从产品中设置信息
	 * @param lmtPrelist
	 * @param prdInfoVO
	 * @param rateSchemeDetailVO
	 * @return
	 */
	public static void setInfoByPrd(LmtPrelist lmtPrelist,PrdInfoVO prdInfoVO,RateSchemeDetailVO rateSchemeDetailVO) {
        if (Objects.nonNull(lmtPrelist)&&Objects.nonNull(prdInfoVO)&&Objects.nonNull(rateSchemeDetailVO)) {
            if(Objects.isNull(lmtPrelist.getIrMode())) {
                lmtPrelist.setIrMode(rateSchemeDetailVO.getRateModel());
            }
        }
    }

}
